﻿/**************************************************************

项目名称 :ThinkWide数据访问基础库
版权所有 (c) 2010 ThinkWide
+-------------------------------------------------------------
项目: 


版本: 1


创建日期:2010-5-3


作者: 章称

+-------------------------------------------------------------
描述:z

更新:   2010-5-3

ToDo:
 * 支持存储过程对象
 * 支持视图对象
 * 支持关联读取和删除(下一版本)

***************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MF.Data.ORMapping;
using MF.Data.Manager;
using MF.Core;
using Web=MF.ORMTest.MFWebServiceReference;
using Wcf = MF.ORMTest.MFWcfServiceReference;
using MF.Data.SupplyInterface;
using MySql.Data.MySqlClient;
namespace MF.ORMTest
{
    public partial class ORMTest : Form
    {
        public ORMTest()
        {
            InitializeComponent();
        }

        private MFDataAdapter da = new MFDataAdapter();
        private MFDataManager dm = new MFDataManager();


        private void btnDirectGetData_Click(object sender, EventArgs e)
        {
            
            DataTable dt = da.GetDataSet(txtSQL.Text).Tables[0];
            dgvMain.DataSource = dt;
        }

        private void btnDirectUpdate_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dgvMain.DataSource;
            dt.TableName = "Test";// "CRUser";//"Test";
            da.SaveDataTable(dt);
        }

        private void btnWebUpdate_Click(object sender, EventArgs e)
        {
            Web.HostServiceSoapClient hs = new Web.HostServiceSoapClient();
            Web.MFServiceInformatiom si = hs.GetServiceInformatiom();
            DataSet ds = new DataSet();
            DataTable dt = (DataTable)dgvMain.DataSource;
            dt.DataSet.Tables.Remove(dt);

            dt.TableName = "Test";
            ds.Tables.Add(dt);
            hs.UpdataDataSet(ds);

            dgvMain.DataSource = ds.Tables[0];
        }

        private void btnWebGetTable_Click(object sender, EventArgs e)
        {
            //MFDataAdapter da = new MFDataAdapter();
            //DataTable dt=da.GetDataTable(txtSQL.Text);

            Web.HostServiceSoapClient hs = new Web.HostServiceSoapClient();
            Web.MFServiceInformatiom si = hs.GetServiceInformatiom();
            DataSet ds = hs.GetDataSet(txtSQL.Text);

            dgvMain.DataSource = ds.Tables[0];

        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            da.ReLoadProvider(cboProvider.Text);
            MFORMappingGlobe.DataAdapter.ReLoadProvider(cboProvider.Text);
            tbgFirst.Text = cboProvider.Text;
           
        }

        private void ORMTest_Load(object sender, EventArgs e)
        {
            cboProvider.DisplayMember = "Name";
            cboProvider.DataSource = da.Configurations;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            dgvMain.DataSource = null;
        }

        private void btnSQL_Click(object sender, EventArgs e)
        {
            MFSQLSelect select = new MFSQLSelect();
            select.Column.Add("sID");
            select.Column.Add("sName");
            select.Column.Add("dCreateDate");
            select.Column.Add("sCreateID");
            select.Where.Add("sID", 0);
            select.Where.Add("sName","OK");
            select.TableName = "Test";
            txtSQLText.Text = select.CreateSQL() + Environment.NewLine;

            txtSQLText.Text += MFSQLSelect.GetSQLSelect("select * from Test where guid=?", "90d9974c-f24a-4a10-80f0-8ec0fc861419");

            DataTable dt = da.GetDataSet(MFSQLSelect.GetSQLSelect("select * from Test where guid=?", "90d9974c-f24a-4a10-80f0-8ec0fc861419")).Tables[0];
            //dgvMain.DataSource = dt;
            dt.TableName = "Test";
            dt.PrimaryKey=new DataColumn[]{dt.Columns[0]};

            MFCommandBuilder cb = new MFCommandBuilder(dt);
            txtSQLText.Text += Environment.NewLine+Environment.NewLine + cb.GetDeleteSQL();
            txtSQLText.Text += Environment.NewLine + cb.GetInsertSQL();

            txtSQLText.Text += Environment.NewLine + cb.GetUpdateSQL();
            txtSQLText.Text += Environment.NewLine + cb.GetSelectSQL();
            //MFDataManager dm = new MFDataManager();
            //DataSet ds= dm.GetDataSet(MFSQLSelect.GetSQLSelect("select * from Test where guid=?", "90d9974c-f24a-4a10-80f0-8ec0fc861419"));
            //ds.WriteXml(@"c:\test.xml",XmlWriteMode.WriteSchema);
           
            //txtSQL.Text = t.sID;
        }

       
        private void btnWCFGet_Click(object sender, EventArgs e)
        {
            Wcf.WCFServiceClient wcf = new Wcf.WCFServiceClient();
            DataSet ds = wcf.GetDataSet(txtSQL.Text);

            dgvMain.DataSource = ds.Tables[0];
        }

        private void btnWCFUpdate_Click(object sender, EventArgs e)
        {
            //Wcf.WCFServiceClient wcf = new Wcf.WCFServiceClient();
            //DataSet ds = new DataSet();
            //DataTable dt = (DataTable)dgvMain.DataSource;
            //dt.DataSet.Tables.Remove(dt);

            //dt.TableName = "Test";
            //ds.Tables.Add(dt);
            //wcf.UpdataDataSet(ds);

            //dgvMain.DataSource = ds.Tables[0];
        }


        private void btnTest_Click(object sender, EventArgs e)
        {
            
        }

        private void btnStore_Click(object sender, EventArgs e)
        {
            

        }

        private void btnView_Click(object sender, EventArgs e)
        {
            
        }

        private void btnGetWebParam_Click(object sender, EventArgs e)
        {
            Web.HostServiceSoapClient hs = new Web.HostServiceSoapClient();
            Web.MFServiceInformatiom si = hs.GetServiceInformatiom();
            //MFDataParameters param=new MFDataParameters();

            Web.MFDataParameter[] dataParamter = new MF.ORMTest.MFWebServiceReference.MFDataParameter[2];
            dataParamter[0] = new Web.MFDataParameter();
            dataParamter[0].ParameterName = "@Value";
            dataParamter[0].DbType = Web.DbType.String;
            dataParamter[0].Direction = Web.ParameterDirection.Input;
            dataParamter[0].Size = 50;
            dataParamter[0].Value = "1234";

            dataParamter[1] = new Web.MFDataParameter();
            dataParamter[1].ParameterName = "@Data";
            dataParamter[1].DbType = Web.DbType.String;
            dataParamter[1].Direction = Web.ParameterDirection.Output;
            dataParamter[1].Size = 50;
            dataParamter[1].Value = null;
            
            //Web.MFDBResuleInfo info=hs.ExecuteSQL("myTestValue", Web.CommandType.StoredProcedure, dataParamter, 30);
            Web.MFDBResuleDataSetInfo info = hs.ExecuteSQLDataSet("myTestValue", Web.CommandType.StoredProcedure, dataParamter, 30);


            MessageBox.Show("Return paramter @Data:" + info.DataParameters[1].Value.ToString());
            dgvMain.DataSource = info.ResultDataSet.Tables[0];

        }

        private void BtnGetWcfParam_Click(object sender, EventArgs e)
        {
            Wcf.WCFServiceClient wcf = new Wcf.WCFServiceClient();
            MFDataParameter[] dataParamter = new MFDataParameter[2];
            dataParamter[0] = new MFDataParameter();
            dataParamter[0].ParameterName = "@Value";
            dataParamter[0].DbType = DbType.String;
            dataParamter[0].Direction = ParameterDirection.Input;
            dataParamter[0].Size = 50;
            dataParamter[0].Value = "1234";

            dataParamter[1] = new MFDataParameter();
            dataParamter[1].ParameterName = "@Data";
            dataParamter[1].DbType = DbType.String;
            dataParamter[1].Direction = ParameterDirection.Output;
            dataParamter[1].Size = 50;
            dataParamter[1].Value = null;

            //Web.MFDBResuleInfo info=hs.ExecuteSQL("myTestValue", Web.CommandType.StoredProcedure, dataParamter, 30);
            MFDBResuleDataSetInfo info = wcf.ExecuteSQLDataSet("myTestValue", CommandType.StoredProcedure, dataParamter, 30);


            MessageBox.Show("Return paramter @Data:"+info.DataParameters[1].Value.ToString());
            dgvMain.DataSource = info.ResultDataSet.Tables[0];

        }

        private void btnGetWcfParamSQL_Click(object sender, EventArgs e)
        {
            Wcf.WCFServiceClient wcf = new Wcf.WCFServiceClient();

            MFDBResuleInfo info = wcf.ExecuteSQL("update Test set sName='2' where Guid='059AE094-136B-4F11-9A8E-126F06DF3C8B'", CommandType.Text, null, 30);
            MessageBox.Show("Execute successful:"+info.Count.ToString());
        }

        private void btnGetWebParamSQL_Click(object sender, EventArgs e)
        {
            Web.HostServiceSoapClient hs = new Web.HostServiceSoapClient();
            Web.MFServiceInformatiom si = hs.GetServiceInformatiom();
                       
            Web.MFDBResuleInfo info = hs.ExecuteSQL("update Test set sName='2' where Guid='059AE094-136B-4F11-9A8E-126F06DF3C8B'", Web.CommandType.Text, null, 30);
            MessageBox.Show("Execute successful:" + info.Count.ToString());

        }

        private void tabOrm_Click(object sender, EventArgs e)
        {

        }

        private void BtnORM_Click(object sender, EventArgs e)
        {
            Test t;
            //t= new Test();
            //t.XGuid = new Guid("94fa9bdf-6d79-49d2-b90d-28f67a78867b");
            //t.Delete();


            t = Test.FindOne("guid=?", "90d9974c-f24a-4a10-80f0-8ec0fc861419");
            t.sName = "2010-1-1";
            t.Update();

            t = Test.FindOne("guid=?", "4a995c4f-1ca2-48f3-b81b-e84fa5bd8711");
            if (t!=null)
                t.Delete();
            //t.SetValue("");


            t = new Test();
            t.XGuid = Guid.NewGuid();
            t.sID = "123";
            t.sName = "da";
            t.Insert();

            List<Test> Tests = Test.FindAll();
            dgvMain.DataSource = Tests;
        }

        private void btnSerializer_Click(object sender, EventArgs e)
        {
            Test t = Test.FindOne("guid=?", "90d9974c-f24a-4a10-80f0-8ec0fc861419");
            
            //MFSerializer.WriteToFile<Test>(t, "c:\\test.xml");
            
            //Test x = MFSerializer.ReadFromFile<Test>(@"c:\test.xml");

            MessageBox.Show(MFSerializer.WriteToString<Test>(t));
        }

        private void BtnDMStoreTest_Click(object sender, EventArgs e)
        {
            MFDataManager dm = new MFDataManager();
            IDbCommand cmd = dm.GetCommand(true);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "myTestValue";

            IDataParameter dp = dm.GetParameter("@Value", System.Data.DbType.String, 50);
            dp.Direction = System.Data.ParameterDirection.Input;
            dp.Value = "1234";
            cmd.Parameters.Add(dp);

            dp = dm.GetParameter("@Data", System.Data.DbType.String, 50);
            dp.Direction = System.Data.ParameterDirection.Output;
            dp.Value = DBNull.Value;
            cmd.Parameters.Add(dp);

            cmd.Prepare();
            cmd.ExecuteNonQuery();
            IDataParameter rp = (IDataParameter)cmd.Parameters[0];
            MessageBox.Show(rp.Value.ToString());
        }

        private void BtnOrmGetTable_Click(object sender, EventArgs e)
        {
            List<xTest> Tests = xTest.FindAll();
            dgvMain.DataSource = Tests;
        }

        private void BtnOrmUpdateTable_Click(object sender, EventArgs e)
        {
            Test t = Test.FindOne("guid=?", "90d9974c-f24a-4a10-80f0-8ec0fc861419");
            t.sName = "2010-1-1";
            t.Update();
        }

        private void BtnOrmDeleteTable_Click(object sender, EventArgs e)
        {
            Test t = Test.FindOne("guid=?", "4a995c4f-1ca2-48f3-b81b-e84fa5bd8711");
            t.Delete();

            
        }

        private void BtnOrmInsertTable_Click(object sender, EventArgs e)
        {
            Test t = new Test();
            t.XGuid = Guid.NewGuid();
            t.sID = "123";
            t.sName = "da";
            t.Insert();
        }

        private void btnOrmView_Click(object sender, EventArgs e)
        {
            List<vwTest> t = vwTest.FindAll();
            dgvMain.DataSource = t;
        }

        private void btnDMGetTable_Click(object sender, EventArgs e)
        {
            dgvMain.DataSource = dm.GetDataSet("select * from test").Tables[0];
        }

        private void btnDMUpdateTable_Click(object sender, EventArgs e)
        {
            //dm.UpdateTable(tb,"test");
        }

        private void btnDMClear_Click(object sender, EventArgs e)
        {
            dgvMain.DataSource = null;
        }

        private void btnOrmStore_Click(object sender, EventArgs e)
        {
            myTestValue t = new myTestValue();
            t.Value = "asdasd";
            t.Execute();
            MessageBox.Show("Return Data:" + t.Data);
            //dgvMain.DataSource = t.ResuleInfo.ResultDataSet.Tables[0];
        }

        private void btnOrmStoreDataSet_Click(object sender, EventArgs e)
        {
            myTestValue t = new myTestValue();
            t.Value = "asdasd";
            t.ExecuteDataSet();
            MessageBox.Show("Return Data:" + t.Data);
            dgvMain.DataSource = t.ResuleInfo.ResultDataSet.Tables[0];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MySqlConnection cn=new MySqlConnection("SERVER=localhost;DATABASE=webdataopsserver;UID=root;PASSWORD=sa");
            MySqlCommand cm = new MySqlCommand("Select * from CRUser", cn);
            MySqlDataAdapter da = new MySqlDataAdapter(cm);
            DataSet ds=new DataSet();
            da.Fill(ds);
            dgvMain.DataSource = ds.Tables[0].DefaultView;
            cn.Open();

            //Update CRUser SET Guid = ?Guid , sUserID = ?sUserID , sPersonID = ?sPersonID , sPassword = ?sPassword , sUserType = ?sUserType , sCreatorID = ?sCreatorID , sModifierID = ?sModifierID , dCreateDate = ?dCreateDate , sDelete = ?sDelete , dModifyDate = ?dModifyDate , sRemark = ?sRemark  Where Guid = ?Original_Guid
            MySqlCommand upcm = new MySqlCommand(
                "Update CRUser SET Guid = ?Guid , sUserID = ?sUserID , sPersonID = ?sPersonID , sPassword = ?sPassword , "+
                "sUserType = ?sUserType , sCreatorID = ?sCreatorID , sModifierID = ?sModifierID , dCreateDate = ?dCreateDate , "+
                "sDelete = ?sDelete , dModifyDate = ?dModifyDate , sRemark = ?sRemark  Where Guid = ?Original_Guid", 
                cn);
            upcm.Parameters.AddWithValue("?Guid", "F0744DBD-AFBB-436F-87CD-6272C5FF8AAC");
            upcm.Parameters.AddWithValue("?sUserID", "123");
            upcm.Parameters.AddWithValue("?sPersonID", "123");
            upcm.Parameters.AddWithValue("?sPassword", "123");
            upcm.Parameters.AddWithValue("?sUserType", "S");
            upcm.Parameters.AddWithValue("?sCreatorID", "123");
            upcm.Parameters.AddWithValue("?sModifierID", "123");
            upcm.Parameters.AddWithValue("?dCreateDate", "2010-01-01");
            upcm.Parameters.AddWithValue("?sDelete", "N");
            upcm.Parameters.AddWithValue("?dModifyDate", "2010-01-01");
            upcm.Parameters.AddWithValue("?sRemark", "456");
            upcm.Parameters.AddWithValue("?Original_Guid", "F0744DBD-AFBB-436F-87CD-6272C5FF8AAC");
            upcm.ExecuteNonQuery();
        }
    }

    

    public static class ZCText
    {
        public static void SetValue(this Test test, String html)
        {
            test.sName = html;
        }
    }


    //Table Object
    public class Test : MFActiveRecordBase<Test>
    {
        [MFField(PrimaryKey = true, FieldName = "Guid")] //Support Alias Name
        public Guid XGuid { get; set; }

        public string sID { get; set; }
        public string sName { get; set; }
        public DateTime? dCreateDate { get; set; }
        public string sCreateID { get; set; }
    }


    //Table Object (Alias Name)
    [MFTable(TableName="test")]
    public class xTest : MFActiveRecordBase<xTest>
    {
        [MFField(AllowNull = false, PrimaryKey = true)]
        public Guid Guid { get; set; }
        public string sID { get; set; }
        public string sName { get; set; }
        public DateTime dCreateDate { get; set; }
        public string sCreateID { get; set; }
    }

    //View Object 
    [MFView(ViewName="vwTest",TableName="Test")]
    public class vwTest : MFActiveRecordBase<vwTest>
    {
        [MFField(PrimaryKey = true, FieldName = "Guid")]
        public Guid XGuid { get; set; }

        public string sID { get; set; }
        public string sName { get; set; }
        public DateTime? dCreateDate { get; set; }
        public string sCreateID { get; set; }
        public string sUserName { get; set; }
    }

    //StoredProcedure Object 
    [MFStored(StoredProcedureName = "myTestValue")]
    public class myTestValue : MFActiveStoredBase<myTestValue>
    {
        [MFStoredParameter(Direction=ParameterDirection.Input,DbType=DbType.String,Size=250)]
        public string Value { get; set; }

        [MFStoredParameter(Direction = ParameterDirection.Output,DbType = DbType.String, Size = 250)]
        public string Data { get; set; }
       
    }
}
